﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;

namespace wpdotnet.Controls
{
    internal class CacheManager
    {

        public static List<string> Keys
        {
            get
            {
                if (_keys == null)
                    _keys = new List<string>();
                return _keys;
            }
            set
            {
                _keys = value;
            }
        }
        private static List<string> _keys = null;

        private static void AddKey(string key)
        {
            if (!Keys.Contains(key))
                Keys.Add(key);
        }
        private static void RemoveKey(string key)
        {
            if (Keys.Contains(key))
                Keys.Remove(key);
        }

        /*
         * Note   The Add and Insert methods have the same signature, but there are subtle differences between them. First, calling the Add method returns an object that represents the cached item, while calling Insert does not. 
         * Second, their behavior is different if you call these methods and add an item to the Cache that is already stored there. The Insert method replaces the item, while the Add method fails.
         */

        public static object Add(string key, object value)
        {
            AddKey(key);

            return HttpRuntime.Cache.Add(key, value, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
        }


        public static bool Contains(string key)
        {
            return HttpRuntime.Cache.Get(key) != null;
        }

        public static int Count()
        {
            return HttpRuntime.Cache.Count;
        }


        public static void Insert(string key, object value)
        {
            AddKey(key);
            HttpRuntime.Cache.Insert(key, value);
        }


        public static T Get<T>(string key)
        {
            return (T)HttpRuntime.Cache.Get(key);
        }


        public static void Remove(string key)
        {
            RemoveKey(key);
            HttpRuntime.Cache.Remove(key);
        }

        public static void RemoveAll()
        {
            foreach (string key in Keys)
            {
                Remove(key);
            }
        }

        public static void RemoveAll(string prefix)
        {
            // get keys where prefix is set to "prefix"
            List<string> existingKeys = Keys.FindAll(delegate(string key) { return key.StartsWith(prefix); });
            foreach (string key in existingKeys)
            {
                Remove(key);
            }
        }

        public object this[string key]
        {
            get
            {
                return HttpRuntime.Cache[key];
            }

            set
            {
                HttpRuntime.Cache[key] = value;
            }
        }



    }
 
}
